package com.zhangyx.crawler.proxy;

import com.google.common.collect.Lists;
import com.zhangyx.crawler.core.HttpClientUtil;
import org.apache.log4j.Logger;

import java.util.List;

public class DynamicHandle {
    private static Logger log = Logger.getLogger(DynamicHandle.class);

    //无忧代理 返回格式
    //36.251.151.5:17912
    //60.18.19.220:55697
    //183.149.53.64:44245
    //183.150.237.174:19736
    //123.163.163.251:33101
    public static List<ProxyEntity> getProxy() {
        List<ProxyEntity> proxyEntities = Lists.newArrayList();
        try {
            String url = "http://api.ip.data5u.com/dynamic/get.html?order=69b2166f0dbdd56d371160c7b32efb5f&sep=3";
            log.debug("get more proxy");
            HttpClientUtil httpClientUtil = HttpClientUtil.POST(url)
                    .start();
            String[] proxiesStr = httpClientUtil.getHtml().split("\r\n");
            if (proxiesStr.length > 0) {
                for (String p : proxiesStr) {
                    String[] proxyStr = p.split(":");
                    if (proxyStr.length != 2) {
                        continue;
                    }
                    ProxyEntity entity = new ProxyEntity();
                    entity.setHost(proxyStr[0]);
                    entity.setPort(Integer.parseInt(proxyStr[1]));
                    entity.setType("http");
                    proxyEntities.add(entity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proxyEntities;
    }
//    45.77.14.47
    public static void main(String[] args) {
        System.out.println(getProxy().size());
    }
}
